package com.zlm.user.emp.controller;

import com.zlm.common.base.BaseController;
import com.zlm.common.base.PageEntity;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.constants.CommonConstants;
import com.zlm.common.dto.JsonResultDto;
import com.zlm.common.dto.userEmp.PositionParamsDto;
import com.zlm.common.dto.userEmp.PositionResponseDto;
import com.zlm.common.entity.UserTokenDO;
import com.zlm.user.emp.service.OrgPositionService;
import com.zlm.user.emp.service.UserTokenService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 企业职位controller
 * @Author: duyufu
 * @Date: 2019/4/24 15:31
 */
@RestController
@RequestMapping(value = "/position")
public class OrgPositionController extends BaseController {

    @Autowired
    private OrgPositionService orgPositionService;
    @Autowired
    private UserTokenService userTokenService;

    /**
     * 获取职位列表
     * @param positionParamsDto
     * @return
     */
    @PostMapping(value = "/getPositionList")
    public JsonResultDto getPositionList(@RequestBody PositionParamsDto positionParamsDto) {
        if (CommonConstants.CHINA.equals(positionParamsDto.getCityName()) ||
                CommonConstants.WHOLE_CHINA.equals(positionParamsDto.getCityName()) ||
                    CommonConstants.CHINA.equals(positionParamsDto.getProvinceName()) ||
                        CommonConstants.WHOLE_CHINA.equals(positionParamsDto.getProvinceName())) {
            positionParamsDto.setCityName(null);
        }
        try {
            PageEntity page = orgPositionService.getPositionList(positionParamsDto);
            return success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(CodeConstants.CODE_BUSY);
        }
    }

    /**
     * 获取职位
     * @param token
     * @param pracId
     * @return
     */
    @GetMapping("/getPosition")
    public JsonResultDto getPosition(String token, Integer pracId) {
        try {
            if (StringUtils.isNotBlank(token)) {
                UserTokenDO loginUser = userTokenService.getLoginUser(token);
                PositionResponseDto positionResponseDto = orgPositionService.getPosition(loginUser.getUserId(),pracId);
                return success(positionResponseDto);
            }
            PositionResponseDto positionResponseDto = orgPositionService.getPosition(pracId);
            return success(positionResponseDto);
        } catch (Exception e) {
            e.printStackTrace();
            return fail(CodeConstants.CODE_BUSY);
        }
    }
}
